package com.jinarusha.shareprayers;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OperationCanceledException;
import android.accounts.AuthenticatorException;
import android.content.Context;
import android.util.Log;
import org.apache.http.cookie.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

public class NetworkCommunication {
	private static final int REGISTRATION_TIMEOUT = 30 * 1000; // ms
	private static final String TOKEN_URI = Config.APP_BASE_URI + "/token";
	private static final String SENDER_URI = Config.APP_BASE_URI + "/sender";
	private static final String LOG_TAG = "shareprayer_networkComm";

	private static DefaultHttpClient httpClient = null;

	public static void sendRegistrationId(String userId, String password,
			String registrationId) {

		try {

			StringBuffer postDataBuilder = new StringBuffer();


			postDataBuilder.append("userId=" + userId);
			postDataBuilder.append("&password=").append(password);
			postDataBuilder.append("&registrationId=").append(registrationId);
			postDataBuilder.append("&phoneNumber=").append(Config.getUSER_PHONE_NUMBER());
//			System.out.println("phoneNumber: "+Config.getUSER_PHONE_NUMBER());
			byte[] postData = postDataBuilder.toString().getBytes("UTF8");
			URL url = new URL(TOKEN_URI);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length",
					Integer.toString(postData.length));
			OutputStream out = conn.getOutputStream();
			out.write(postData);
			out.close();
			conn.disconnect();

		} catch (UnsupportedEncodingException ex) {
			Log.e(LOG_TAG, "UnsupportedEncodingException", ex);

		} catch (IOException ex) {
			Log.e(LOG_TAG, "IOException", ex);

		}

	}

	@SuppressWarnings("finally")
	public static void sendMessage(String senderId, String targetId,
			String message) {

		try {
			StringBuffer postDataBuilder = new StringBuffer();
			Log.i(LOG_TAG, "about to send message");
			Log.i(LOG_TAG, "senderId: " + senderId);
			Log.i(LOG_TAG, "targetId: " + targetId);

			postDataBuilder.append("senderId=" + senderId);
			postDataBuilder.append("&targetId=" + targetId);
			postDataBuilder.append("&message="
					+ URLEncoder.encode(message, "UTF-8"));
			Log.i(LOG_TAG, "total message: " + postDataBuilder.toString());
			byte[] postData = postDataBuilder.toString().getBytes("UTF8");

			URL url = new URL(SENDER_URI);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length",
					Integer.toString(postData.length));

			OutputStream out = conn.getOutputStream();
			out.write(postData);
			out.close();

			conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			System.out
					.println("sendRegistrationId: output msg" + br.readLine());
			conn.disconnect();
		} catch (UnsupportedEncodingException ex) {
			Log.e(LOG_TAG, "UnsupportedEncodingException", ex);

		} catch (IOException ex) {
			Log.e(LOG_TAG, "IOException", ex);

		}

	}

}
